﻿
using Newtonsoft.Json;
using RestSharp;
using System.Collections;

namespace H.Y.Clipboard
{
	class HttpClient
	{
        public interface IHttpResponseCallback
        {
            void onResponse(string responseData);
        }

        IHttpResponseCallback responseCallback;
        string host;
        int port;

        public HttpClient(string host, int port, IHttpResponseCallback responseCallback)
        {
            this.responseCallback = responseCallback;
            this.host = host;
            this.port = port;
        }

        public void send(string jsonData)
        {
            Hashtable ht = JsonConvert.DeserializeObject<Hashtable>(jsonData);
            RestClient client = new RestClient(string.Format("http://{0}:{1}", host, port));
            RestRequest request = new RestRequest(APIs.getInstance().getAPI((string) ht[Macros.DBTags.ACTION]), Method.POST);
            request.AddParameter("application/json", jsonData, ParameterType.RequestBody);

            client.ExecuteAsync(request, response =>
            {
                responseCallback.onResponse(response.Content);
            });
        }

    }
}
